const app = getApp()

Page({
  data: {
    nickName: '',
    avatar: '',
  },
  onLoad() {
    // 获取用户的信息，基于用户信息回显页面
    this.getUserProfile()
  },
  onShow() {
    this.getUserProfile()
  },
  async getUserProfile() {
    if (this.loading) return
    this.loading = true
    wx.http
      .get('/userInfo')
      .then((res) => {
        this.loading = false
        if (res.code !== 10000) {
          return wx.utils.toast()
        }
        const userinfo = {
          nickName: res.data.nickName,
          avatar: res.data.avatar,
        }
        this.setData({ ...userinfo })
        // 用户信息，设置到应用实例对象上
        app.userinfo = userinfo
      })
      .finally(() => {
        console.log('finally')
        this.loading = false
      })
  },
})
